package wyy

import (
	"api-go/serializer"
	"api-go/util"
	"fmt"
	"net/url"
	"strconv"
)

// 搜索所需信息
type Search struct {
	Key    string `form:"key"`
	Limit  string `form:"limit"`
	Offset string `form:"offset"`
	Type   string `form:"type"`
}

//获取搜索信息
func (service *Search) SearchInfo() *serializer.Response {

	if val := service.Key; val != "" {
		service.Key = val
	} else {
		service.Key = "周杰伦"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "30"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	offsetNum, _ := strconv.Atoi(service.Offset)
	limitNum, _ := strconv.Atoi(service.Limit)
	service.Offset = strconv.Itoa((offsetNum - 1) * limitNum)

	// 关键字搜索（1: 单曲, 10: 专辑, 100: 歌手, 1000: 歌单, 1002: 用户, 1004: MV, 1006: 歌词, 1009: 电台, 1014: 视频）
	if val := service.Type; val != "" {
		service.Type = val
	} else {
		service.Type = "1"
	}
	//1: 单曲, 2: 专辑, 3: 歌手, 4: 用户, 5: MV, 6: 歌词, 7: 电台, 8: 视频，9：歌单
	switch service.Type {
	case "1":
		service.Type = "1"
	case "2":
		service.Type = "10"
	case "3":
		service.Type = "100"
	case "4":
		service.Type = "1002"
	case "5":
		service.Type = "1004"
	case "6":
		service.Type = "1006"
	case "7":
		service.Type = "1009"
	case "8":
		service.Type = "1014"
	case "9":
		service.Type = "1000"
	}
	//此处需要对中文进行转码，否则会无法请求第三方api
	service.Key = url.QueryEscape(service.Key)
	url := "https://music.163.com/api/search/get?s=" + service.Key + "&limit=" + service.Limit + "&offset=" + service.Offset + "&type=" + service.Type

	fmt.Println(url)
	res, err := util.Get(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取热搜
func (service *Search) SearchHot() *serializer.Response {

	url := "https://music.163.com/api/search/hot?type=1111"

	res, err := util.Get(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}
